﻿using System.Threading;
using System.IO.Pipes;

namespace SimplePipeHelper.Common
{
    internal class PipeAsyncState
    {
        public PipeStream PipeStream { get; set; }
        public byte[] Buffer { get; set; }
        public ManualResetEvent EventHandle { get; set; }

        private readonly object _asyncStateLockHelper = new object();

        public void SetEvent()
        {
            lock (_asyncStateLockHelper)
            {
                EventHandle?.Set();
            }
        }

        public bool ResetEvent()
        {
            bool ret;
            lock (_asyncStateLockHelper)
            {
                ret = EventHandle?.Reset() ?? false;
            }

            return ret;
        }

        public void Dispose()
        {
            EventHandle?.Dispose();
            EventHandle = null;
        }
    }
}
